package com.lyf.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lyf.dto.SetmealDTO;
import com.lyf.dto.SetmealPageQueryDTO;
import com.lyf.mapper.admin.SetmealDishMapper;
import com.lyf.mapper.admin.SetmealMapper;
import com.lyf.pojo.Setmeal;
import com.lyf.pojo.SetmealDish;
import com.lyf.result.PageResult;
import com.lyf.service.SetmealService;
import com.lyf.utils.AliOssUtils;
import com.lyf.vo.DishItemVO;
import com.lyf.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private AliOssUtils aliOssUtils;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 新增套餐数据
        setmealMapper.insert(setmeal);
        // 新增套餐菜品数据
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        // 查询套餐数据
        List<Setmeal> setmeals = setmealMapper.selectByIds(ids);
        // 删除套餐数据
        setmealMapper.deleteBatch(ids);
        // 删除图片
        for(Setmeal setmeal : setmeals){
            try {
                aliOssUtils.deleteFile(setmeal.getImage());
            }catch (Exception e){
                System.err.println("图片删除失败: " + setmeal.getImage());
            }
        }

    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 修改套餐
        setmealMapper.update(setmeal);
        // 删除套餐中的菜品数据
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);
        // 插入新的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        // 查询相关套餐数据
        Page<SetmealVO> results = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(results.getTotal(), results.getResult());
    }

    /**
     * 根据id查询套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDishes(Long id) {
        // 查询套餐数据
        SetmealVO setmealVO = setmealMapper.selectById(id);

        // 查询套餐中的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.selectSetmealDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }


    /**
     * 套餐起售停售
     * @param setmeal
     */
    @Override
    public void startOrStop(Setmeal setmeal) {
        setmealMapper.update(setmeal);
    }

    @Override
    public List<SetmealVO> getByCategoryId(Long id) {
        List<SetmealVO> list = setmealMapper.getByCategoryId(id);
        return list;
    }
}
